//
//  KSNavigationController.m
//  KQVH_G
//
//  Created by 郭树清 on 16/12/6.
//  Copyright © 2016年 KuoShuChing. All rights reserved.
//

#import "KSNavigationController.h"

@interface KSNavigationController ()<UINavigationControllerDelegate>

@property (nonatomic,getter=isPushing) BOOL pushing;   // 记录 push 的标记，用于：从源头上解决，页面的多 push 问题
/**
 * lnn
 */
@property (nonatomic,weak) UIViewController *viewController;
@end

@implementation KSNavigationController

/**
 *  当第一次来到这个类的时候会调用一次.
 *  做一些类的初始化设置最合适了!!
 */
+(void)initialize
{

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate =self;
    //设置导航栏  通过appearance 设置!!!
    if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0f) {
        UINavigationBar * bar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[self class]]];
//        [bar setTitleTextAttributes:
//         @{NSFontAttributeName:[UIFont systemFontOfSize:17],
//           NSForegroundColorAttributeName:[UIColor hexStringToColor:@"#333333"]}];
//        bar.barTintColor = [UIColor hexStringToColor:@"#F8F8F8"];
    }else{
        [self.navigationBar setBarTintColor:[UIColor whiteColor]];
        [[UINavigationBar appearance] setTranslucent:NO];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //判断设置
    if (self.childViewControllers.count > 0) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        //设置文字颜色
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        //设置内容靠左显示
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //设置内边距
        [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [btn setImage:[UIImage imageNamed:@"BackA"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"BackA"] forState:UIControlStateHighlighted];
//        [btn setTitle:@"返回" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        btn.size = CGSizeMake(40, 30);
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
        viewController.hidesBottomBarWhenPushed = YES;
    }
    //~下面代码是解决页面的多次 push 使用~
    if (self.pushing ==  YES) {
        NSLog(@"被拦截");
        return;
    }else{
        NSLog(@"被push");
        self.pushing = YES;
    [super pushViewController:viewController animated:animated];
    }
}
//导航控制器的代理方法的实现（为了解决页面 的多次 push 的问题）
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    self.pushing = NO;
}
-(void)back
{
    [self popViewControllerAnimated:YES];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//lnn
//- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
//
//    //    self.navigationBar.tintColor = [UIColor whiteColor];//导航字体颜色
//    //    //    self.navigationBar.barTintColor = RGBACOLOR;//导航栏底色
//    //    [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
//    // 设置左上角返回按钮
//    UIButton *backBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
//    backBtn.size = CGSizeMake(70, 44);
//    //    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
//    [backBtn setImage:[UIImage imageNamed:@"BackA"] forState:UIControlStateNormal];
//    [backBtn setImage:[UIImage imageNamed:@"BackA"] forState:UIControlStateHighlighted];
//    // 设置按钮内容左对齐
//    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    // 让按钮的内容往左边偏移10
//    backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -7, 0, 0);
//    backBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
//    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
//    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//    // 隐藏tabbar
//    if (self.viewControllers.count > 0) {//隐藏tabbar
//        viewController.hidesBottomBarWhenPushed = YES;
//    }else{
//        backBtn.hidden = YES;//四个主页隐藏返回按钮
//    }
//    [super pushViewController:viewController animated:animated];
//    self.viewController = viewController;
//}
//
//- ( UIStatusBarStyle ) preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent ; //状态栏样式
//}
//- ( BOOL ) prefersStatusBarHidden{
//    return NO ; //状态栏是否隐藏
//}
//- (void)dealloc{
//
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}
//
//- (void)back{
//    [self popViewControllerAnimated:YES];
//}
@end
